package xyz.ibenben.zhongdian.common.intercepter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import xyz.ibenben.zhongdian.system.entity.*;
import xyz.ibenben.zhongdian.system.entity.sys.SysUser;
import xyz.ibenben.zhongdian.system.service.*;
import xyz.ibenben.zhongdian.system.service.impl.RedisService;
import xyz.ibenben.zhongdian.system.service.sys.SysUserService;

import java.util.List;

/**
 * 服务启动执行
 * @author  chenjian
 * @since	2016年1月9日
 */
@Slf4j
@Component
public class MyStartupRunner implements CommandLineRunner {
	@Autowired
	private ChinaProvinceService chinaProvinceService;
	
	@Autowired
	private ChinaCityService chinaCityService;
	
	@Autowired
	private ChinaRegionService chinaRegionService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private RedisService redisService;

	/**
	 * 服务启动执行，执行加载数据等操作
	 * @param args 参数
	 * @throws Exception 异常
	 */
	@Override
	public void run(String... args) throws Exception {
		log.info(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");
		//获取所有省份记录
		List<ChinaProvince> pList = chinaProvinceService.selectAll();
		for(ChinaProvince pro: pList){
			redisService.set(pro.getProvinceCode().toString(), pro.getProvinceName());
			List<ChinaCity> list = chinaCityService.findListByProvinceCode(pro.getProvinceCode());
			//把城市和省份放到Redis里
			redisService.set("city" + pro.getProvinceCode(), list);
		}
		//把省份放到Redis里
		redisService.set("provinceList", pList);
		//获取所有城市记录
		List<ChinaCity> cList = chinaCityService.selectAll();
		for(ChinaCity city: cList){
			redisService.set(city.getCityCode().toString(), city.getCityName());
			List<ChinaRegion> list = chinaRegionService.findListByCityCode(city.getCityCode());
			//把区县和城市放到Redis里
			redisService.set("region" + city.getCityCode(), list);
		}
		//把城市放到Redis里
		redisService.set("cityList", cList);
		//获取所有区县记录
		List<ChinaRegion> rList = chinaRegionService.selectAll();
		for(ChinaRegion region: rList){
			//把区县和区县名放到Redis里
			redisService.set(region.getRegionCode().toString(), region.getRegionName());
		}
		//把区县放到Redis里
		redisService.set("regionList", rList);
		//获取所有用户记录
		List<SysUser> users = sysUserService.selectAll();
		for(SysUser user: users){
			//把用户放到Redis里id是主键
			redisService.set(user.getId().toString(), user.getUsername());
		}
	}

}
